%% Author: GUWENQING
%% Created: 2012-4-7
%% Description: TODO: Add description to gossip_tester
-module(gossip_tester).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([test_correct/2, test_performance_balance/2, test_performance_serial_target/1, test_performance_serial_t/1, get_average_target/2, get_average_t/2, get_average/2]).

%%
%% API Functions
%%

%%
%% Test correctness of the gossip implementation for resource allocation.
%%
%% Parameters:
%% 	T: Number of nodes
%% 	Target: Largest possible initial CPU configuration value in the node
%%
test_correct(T, Target) ->
	{Inits, Result} = gossip:run(T, Target),
	Return = [{ID, lists:nth(ID, Inits), CPU} || {ID, CPU, _Stat} <- Result],
	io:format("[~p,~p,~n~p]~n", [T, Target, Return]).

%%
%% Test performance balance of the gossip implementation for resource allocation.
%%
%% Parameters:
%% 	T: Number of nodes
%% 	Target: Largest possible initial CPU configuration value in the node
%%
test_performance_balance(T, Target) ->
	{_Inits, Result} = gossip:run(T, Target),
	Return = [{ID, ActiveReceiveNormal, ActiveReceiveReject, PassiveSendNormal, PassiveSendReject, ActiveReceiveNormal + ActiveReceiveReject + PassiveSendNormal + PassiveSendReject} || {ID, _CPU, {{_ActiveSend, ActiveReceiveNormal, ActiveReceiveReject, _ActiveReceiveTimeout}, {PassiveSendNormal, PassiveSendReject}}} <- Result],
	io:format("[~p,~p,~n~p]~n", [T, Target, Return]).


%%
%% Test performance of the gossip implementation for resource allocation in change of Target in serial.
%%
%% Parameters:
%% 	T: Number of nodes
%%  Target: Largest possible initial CPU configuration value in the node
%% 	Target = 10000 * [1..100]
%%
test_performance_serial_target(T) ->
	Bases = lists:seq(1,100),
	Targets = [10000 * Times1 || Times1 <- Bases],
	Avgs = [gossip_tester:get_average_target(T, Target) || Target <- Targets],
	io:format("[~p,~n~p]~n", [T, Avgs]).

%%
%% Test performance of the gossip implementation for resource allocation in change of T in serial.
%%
%% Parameters:
%% 	T: Number of nodes
%%  Target: Largest possible initial CPU configuration value in the node
%% 	T = 10 * [1..100]
%%
test_performance_serial_t(Target) ->
	Bases = lists:seq(1,100),
	Ts = [10 * Times1 || Times1 <- Bases],
	Avgs = [gossip_tester:get_average_t(T, Target) || T <- Ts],
	io:format("[~p,~n~p]~n", [Target, Avgs]).

get_average(T, Target) -> 	{_Inits, Result} = gossip:run(T, Target),
	ActiveReceiveNormalList = [ActiveReceiveNormal || {_ID, _CPU, {{_ActiveSend, ActiveReceiveNormal, _ActiveReceiveReject, _ActiveReceiveTimeout}, {_PassiveSendNormal, _PassiveSendReject}}} <- Result],
	ActiveReceiveNormalAvg = lists:sum(ActiveReceiveNormalList)/T,
	ActiveReceiveRejectList = [ActiveReceiveReject || {_ID, _CPU, {{_ActiveSend, _ActiveReceiveNormal, ActiveReceiveReject, _ActiveReceiveTimeout}, {_PassiveSendNormal, _PassiveSendReject}}} <- Result],
	ActiveReceiveRejectAvg = lists:sum(ActiveReceiveRejectList)/T,
	PassiveSendNormalList = [PassiveSendNormal || {_ID, _CPU, {{_ActiveSend, _ActiveReceiveNormal, _ActiveReceiveReject, _ActiveReceiveTimeout}, {PassiveSendNormal, _PassiveSendReject}}} <- Result],
	PassiveSendNormalAvg = lists:sum(PassiveSendNormalList)/T,
	PassiveSendRejectList = [PassiveSendReject || {_ID, _CPU, {{_ActiveSend, _ActiveReceiveNormal, _ActiveReceiveReject, _ActiveReceiveTimeout}, {_PassiveSendNormal, PassiveSendReject}}} <- Result],
	PassiveSendRejectAvg = lists:sum(PassiveSendRejectList)/T,
	TotalList = [ActiveReceiveNormal + ActiveReceiveReject + PassiveSendNormal + PassiveSendReject || {_ID, _CPU, {{_ActiveSend, ActiveReceiveNormal, ActiveReceiveReject, _ActiveReceiveTimeout}, {PassiveSendNormal, PassiveSendReject}}} <- Result],
	TotalAvg = lists:sum(TotalList)/T,
	{ActiveReceiveNormalAvg, ActiveReceiveRejectAvg, PassiveSendNormalAvg, PassiveSendRejectAvg, TotalAvg}.

get_average_target(T, Target) ->
	{ActiveReceiveNormalAvg, ActiveReceiveRejectAvg, PassiveSendNormalAvg, PassiveSendRejectAvg, TotalAvg} = get_average(T, Target),
	{Target, ActiveReceiveNormalAvg, ActiveReceiveRejectAvg, PassiveSendNormalAvg, PassiveSendRejectAvg, TotalAvg}.

get_average_t(T, Target) ->
	{ActiveReceiveNormalAvg, ActiveReceiveRejectAvg, PassiveSendNormalAvg, PassiveSendRejectAvg, TotalAvg} = get_average(T, Target),
	{T, ActiveReceiveNormalAvg, ActiveReceiveRejectAvg, PassiveSendNormalAvg, PassiveSendRejectAvg, TotalAvg}.

